//
//  MCChooseAreaCell.swift
//  StoreManage
//
//  Created by MC on 2018/9/20.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit


typealias MCChooseAreaCellClosure = (MCChooseAreaModel) -> Void

class MCChooseAreaCell: UITableViewCell {
    
    
    var closure : MCChooseAreaCellClosure?
    
    
    var model = MCChooseAreaModel() {
        didSet {
            nameLabel.text = model.proName
            
            citysLabel.text = model.selectedCitysStr
            
            if model.selectedCityModels.count == 0 {
               statusButton.isSelected = false
                model.b = false
            } else {
                statusButton.isSelected = true
                model.b = true
            }
        }
    }
    
    
    public class func customCell(tableView:UITableView) -> MCChooseAreaCell {
        
        let reuseIdentifier = "cell";
        var cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier)
        
        cell = MCChooseAreaCell(style: .default, reuseIdentifier: reuseIdentifier)
        
        return cell as! MCChooseAreaCell
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle = UITableViewCellSelectionStyle.none
        
        self.addSubview(statusButton)
        self.addSubview(nameLabel)
        self.addSubview(citysLabel)
        self.addSubview(lineView)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        statusButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(0)
            make.top.equalTo(0)
            make.width.height.equalTo(50)
        }
        
        nameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(statusButton.snp.right).offset(0)
            make.centerY.equalTo(statusButton)
        }
        
        citysLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(statusButton.snp.right).offset(0)
            make.right.equalTo(self).offset(-20)
            make.top.equalTo(statusButton.snp.bottom).offset(0)
            make.bottom.lessThanOrEqualTo(-15)
        }
        
        lineView.snp.remakeConstraints { (make) ->Void in
            make.left.bottom.right.equalTo(self)
            make.height.equalTo(1)
        }
        
    }
    
    @objc func statusButtonClicked() {

//        if statusButton.isSelected {
//            model.selectedCityModels = [MCChooseAreaCityModel]()
//        } else {
//            closure?(model)
//        }
        
        closure?(model)

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    lazy var statusButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.setImage(#imageLiteral(resourceName: "GoodsManage_notSelected"), for: .normal)
        button.setImage(#imageLiteral(resourceName: "GoodsManage_selected"), for: .selected)
        button.addTarget(self, action: #selector(statusButtonClicked), for: .touchUpInside)
        return button
    }()
    
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(14)
        label.textColor = UIColor.MCWords
        label.text = "XXX"
        return label
    }()
    
    lazy var citysLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(12)
        label.textColor = UIColor.MCGray_middle
        label.numberOfLines = 0
        return label
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
    
}
